tests/libtest: Allow appending actions to be run on EXIT
authorDan Nicholson <nicholson@endlessm.com>
Thu, 13 Jun 2019 20:57:17 +0000 (15:57 -0500)
committerAtomic Bot <atomic-devel@projectatomic.io>
Wed, 19 Jun 2019 17:30:24 +0000 (17:30 +0000)
Currently if a test script adds a trap on `EXIT` to run some cleanup, it
will stomp on the existing trap to run `save_core()`. Allow for scripts
to append actions that will run on exit by introducing an array that
will be iterated over by a single exit runner.

Closes: #1799
Approved by: cgwalters

tests/libtest.sh
tests/test-repo-finder-mount-integration.sh
tests/test-rofiles-fuse.sh

index 99d7c9672791e2c93c96fa5b562b0248ce8cde0a..e70b7b876c36b2c7dfbc56e15031c8ff631beee0 100755 (executable)
@@ -34,13 +34,24 @@ else
 fi
 . ${test_srcdir}/libtest-core.sh
 
+# Array of expressions to execute when exiting. Each expression should
+# be a single string (quoting if necessary) that will be eval'd. To add
+# a command to run on exit, append to the libtest_exit_cmds array like
+# libtest_exit_cmds+=(expr).
+libtest_exit_cmds=()
+run_exit_cmds() {
+  for expr in "${libtest_exit_cmds[@]}"; do
+    eval "${expr}" || true
+  done
+}
+trap run_exit_cmds EXIT
+
 save_core() {
   if [ -e core ]; then
     cp core "$test_srcdir/core"
   fi
 }
-
-trap save_core EXIT;
+libtest_exit_cmds+=(save_core)
 
 test_tmpdir=$(pwd)
 
index 243df5912333276da91911fdc19f06f5531d4069..9ecc4cd639e307bea8d6dbad87aae4ef2da59d48 100755 (executable)
@@ -55,7 +55,7 @@ _mount_cleanup () {
 
 case "${TEST_SKIP_CLEANUP:-}" in
     no|"")
-        trap _mount_cleanup EXIT
+        libtest_exit_cmds+=(_mount_cleanup)
         ;;
     err)
         trap _mount_cleanup ERR
index 7b7474d081ca6a44ca020212892663345e9c333f..1e09711cc6eb3d18a48cc41d3bd8702131bbf8b4 100755 (executable)
@@ -41,7 +41,7 @@ rofiles-fuse checkout-test2 mnt
 cleanup_fuse() {
     fusermount -u ${test_tmpdir}/mnt || true
 }
-trap cleanup_fuse EXIT
+libtest_exit_cmds+=(cleanup_fuse)
 assert_file_has_content mnt/firstfile first
 echo "ok mount"